package com.yonyou.pmclouds.plan.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.measureindex.entity.MeasureIndexHistoryVO;
import com.yonyou.pmclouds.measureindex.service.rmiitf.MeasureIndexHisQuery;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.plan.entity.PlanProgressHistoryVO;
import com.yonyou.pmclouds.plan.mapper.PlanProgressHistoryMapper;
import com.yonyou.pmclouds.plan.mapper.ScheduleItemMapper;
import com.yonyou.pmclouds.plan.rmiitf.IScheduleProgressHistoryQueryService;
import jodd.util.ArraysUtil;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = IScheduleProgressHistoryQueryService.class)
public class ScheduleProgressHistoryQueryServiceImpl implements IScheduleProgressHistoryQueryService {

    @Autowired
    private PlanProgressHistoryMapper historyMapper;
    @Autowired
    private ScheduleItemMapper scheduleItemMapper;
    @Autowired
    private PictureRemoteService picService;
    @Autowired
    private MeasureIndexHisQuery measureIndexHisQuery;

    @Override
    public PlanProgressHistoryVO[] queryByPkPlans(String[] pkPlans) throws BusinessException {
        return historyMapper.queryProgressHistoryByPlans(pkPlans);
    }

    @Override
    public Map<String, List<PlanProgressHistoryVO>> queryByPkPlansWithMap(String[] pkPlans) throws BusinessException {
        PlanProgressHistoryVO[] historyVOS = this.queryByPkPlans(pkPlans);

        if(historyVOS == null || historyVOS.length <= 0){
            return new HashMap<>();
        }

        Map<String, List<PlanProgressHistoryVO>> pkPlanWithHistoryMap = new HashMap<>();
        for(PlanProgressHistoryVO historyVO : historyVOS){
            List<PlanProgressHistoryVO> historyVOList = pkPlanWithHistoryMap.get(historyVO.getPkPlan());
            if(historyVOList == null){
                historyVOList = new ArrayList<>();
                pkPlanWithHistoryMap.put(historyVO.getPkPlan(), historyVOList);
            }
            historyVOList.add(historyVO);
        }

        return pkPlanWithHistoryMap;
    }

    @Override
    public PlanProgressHistoryVO[] queryProgressHisByPlan(String pkOriginal) throws BusinessException {
        //根据计划查询所有历史版本计划id
        String[] pks = scheduleItemMapper.queryPksByPkOriginal(pkOriginal);

        PlanProgressHistoryVO[] planProgressHistoryVOS = historyMapper.queryProgressHistoryByPlans(pks);

        if (!ArrayUtils.isEmpty(planProgressHistoryVOS)) {

            String[] ids = new String[planProgressHistoryVOS.length];
            for (int i = 0; i < planProgressHistoryVOS.length; i++) {
                ids[i] = planProgressHistoryVOS[i].getPkProgressHis();
            }
            // 查询图片
            Map<String, List<String>> picsMap = picService.queryPicsByPkEntitys(ids);
            if (picsMap != null && !picsMap.isEmpty()) {
                for (PlanProgressHistoryVO history : planProgressHistoryVOS) {
                    List<String> strings = picsMap.get(history.getPkProgressHis());
                    if (strings != null && !strings.isEmpty()) {
                        history.setPictures(strings.toArray(new String[0]));
                    } else {
                        history.setPictures(new String[0]);
                    }
                }
            }

            //查询指标记录
            Map<String, List<MeasureIndexHistoryVO>> measureIndexHisMap = measureIndexHisQuery.queryByPkProgressHiss(ids);

            if (measureIndexHisMap != null) {
                for (PlanProgressHistoryVO history : planProgressHistoryVOS) {
                    List<MeasureIndexHistoryVO> measureIndexHistoryVOS = measureIndexHisMap.get(history.getPkProgressHis());
                    if (measureIndexHistoryVOS != null) {
                        history.setMeasureIndexHistoryVOS(measureIndexHistoryVOS.toArray(new MeasureIndexHistoryVO[measureIndexHistoryVOS.size()]));
                    }
                }
            }
        }
        return planProgressHistoryVOS;
    }

    @Override
    public PlanProgressHistoryVO queryLastProgressHisByPkPlans(String[] pkPlans) throws BusinessException {

        PlanProgressHistoryVO[] planProgressHistoryVOS = historyMapper.queryLastProgressHistoryByPkPlans(pkPlans);

        if (!ArrayUtils.isEmpty(planProgressHistoryVOS)) {
            String[] ids = new String[planProgressHistoryVOS.length];
            for (int i = 0; i < planProgressHistoryVOS.length; i++) {
                ids[i] = planProgressHistoryVOS[i].getPkProgressHis();
            }
            // 查询图片
            Map<String, List<String>> picsMap = picService.queryPicsByPkEntitys(ids);
            if (picsMap != null && !picsMap.isEmpty()) {
                for (PlanProgressHistoryVO history : planProgressHistoryVOS) {
                    List<String> strings = picsMap.get(history.getPkProgressHis());
                    if (strings != null && !strings.isEmpty()) {
                        history.setPictures(strings.toArray(new String[0]));
                    } else {
                        history.setPictures(new String[0]);
                    }
                }
            }
            return planProgressHistoryVOS[0];
        }


        return null;
    }
}
